//
//  MusicListViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/6/1.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MusicListViewController.h"

#import "DataBaseHandler.h"
#import "SingerModel.h"
#import "MusicListDetailViewController.h"

@interface MusicListViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic,retain) UITableView *table;
@property (nonatomic,retain) UIImageView *image0;

@end

@implementation MusicListViewController


- (void)dealloc
{
    [_array release];
    [_table release];
    [_image0 release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的歌单";
    [self createbutton];
    [self takeout];
    [self createTableView];
}

- (void)takeout
{
    NSArray *array = [NSArray array];
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    array =  [dbHandler selecttableName];
    [self.array addObjectsFromArray:array];
}


- (void)createbutton
{

    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 30, 30)];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    UIImage *image = [UIImage imageNamed:@"62.png"];
    [button setImage:image forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
}


- (void)createTableView
{
    if ([self.array count] == 0) {
        
        UIImageView *image0 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        UIImage *image = [UIImage imageNamed:@"90.jpg"];
        [image0 setImage:image];
        [self.view addSubview:image0];
    }else{
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    }
}

- (void)buttonAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"新建歌单" message:@"" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.delegate = self;
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
    [alert release];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField *field = [alertView textFieldAtIndex:0];
    NSString *str = field.text;
    
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    [dbHandler createTableWithString:str];
    
    [self.array addObject:str];
    [self.image0 setHidden:YES];
        
    [self createTableView];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellident = @"musicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellident];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellident];
    }
    
    NSString *str = [self.array objectAtIndex:indexPath.row];
    cell.textLabel.text = str;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *str = [self.array objectAtIndex:indexPath.row];
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    NSArray *arr = [dbHandler selectAllWithstr:str];
    MusicListDetailViewController *musicDetail = [[MusicListDetailViewController alloc]init];

    musicDetail.str = str;
    musicDetail.array = arr;
    [self.navigationController pushViewController:musicDetail animated:YES];
    
}

// 设置cell 的编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (indexPath.row == 0) {
    return UITableViewCellEditingStyleDelete;
    //    }else{
    //        return UITableViewCellEditingStyleInsert;
    //    }
}

// 提交编辑结果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSString *str = [self.array objectAtIndex:indexPath.row];
        DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
        [dbHandler deleteWithtable:str];
        
        [self.array removeObjectAtIndex:indexPath.row];
        [self.table reloadData];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.frame = CGRectMake(-320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    
    [UIView animateWithDuration:0.7 animations:^{
        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    } completion:^(BOOL finished) {
        ;
    }];
    
    
    //        CATransform3D rotation;
    //        rotation = CATransform3DMakeRotation( (20.0*M_PI)/180, 0.0, 0.7, 0.4);
    //        rotation.m33 = 0.1;
    //
    //        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    //        cell.layer.shadowOffset = CGSizeMake(10, 10);
    //        cell.alpha = 0;
    //        cell.layer.transform = rotation;
    //        cell.layer.anchorPoint = CGPointMake(0, 0.5);
    //
    //
    //        [UIView beginAnimations:@"rotation" context:NULL];
    //        [UIView setAnimationDuration:1];
    //        cell.layer.transform = CATransform3DIdentity;
    //        cell.alpha = 1;
    //        cell.layer.shadowOffset = CGSizeMake(0, 0);
    //        [UIView commitAnimations];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
